<?php
	include_once("cmsConfig.php");
	
	//$tableWriter = new CmsTableWriter(CMS_FORMAT, $db);
	
	/*
		$documentCode is the id of the document we are indexing
		$columnList is the list of columns in the database to get the keywords out of
		$tableWriter is the tableWriter class that handles the database stuff..
	*/
	function indexDocument($documentCode, $columnList, $tableWriter) {
		$message = "<pre>\n";
		$noiseWords = file (CMS_SEARCH_NOISEWORDS);
		$db = $tableWriter->getTableWriterDB();
		$formDescriptior = new CmsTableWriter("formdescriptor", $db);
		$formElement = new CmsTableWriter("formelement", $db);
		$querytable = new CmsTableWriter("content", $db);
		$countrytable = new CmsTableWriter("country", $db);
		$manytomanydata = new CmsTableWriter(CMS_MANYDATA, $db);
		if (($row = $tableWriter->readItem($documentCode))) {
			$message .= "documentCode:$documentCode\n";
			foreach ( $columnList as $column ) {
				$message .= "\tindexing column $column:";
				$pattern = array();
				
				//Must pull out those columns that are selects and code lists
				//Assumes that the title is the correct field to index 
				//and it is from the content table
				$formDescription = $formDescriptior->readItem($row->formid);
				$elementDescription = $formElement->readItem($formDescription->$column);//echo "here!".$elementDescription->elementtype;
				
				if($elementDescription->elementtype == "3" ) {  
					if((substr_count($elementDescription->sourcequery, "SELECT iso_3166_1_alpha_3, name FROM country") ) > 0 ) {
						//Use country table instead and pull out country name
						$countrytable->resetCustomIterator("Where iso_3166_1_alpha_3 = '".$row->{ $column }."' ");
						$codeWordRow = $countrytable->nextItem();
						$codeWord = $codeWordRow->name;
					} else {
						$codeWordRow = $querytable->readItem($row->{ $column });
						$codeWord = $codeWordRow->title;
					}
				} elseif ( $elementDescription->elementtype == "9") {
						//pull out the titles of the related items
						//use the element id and document code to find the related document codes
						$manytomanydata->resetCustomIterator("Where elementid = '".$elementDescription->elementid."' and parentcode = '".$documentCode."' ");
						while ($relatedRow = $manytomanydata->nextItem()) {
							$relatedDocument = $querytable->readItem($relatedRow->relatedcode);
							$codeWord .= " ".$relatedDocument->title." ";
						}
						
				} else {
					$codeWord = '';
				}
				
				if(strlen($codeWord) > 0) {
					$filtered = strtolower ($codeWord);
				} else {
					$filtered = strtolower ($row->{ $column } );
				}
				$filtered = strip_tags($filtered);
				
				array_push($pattern, "/^/");

				// we replace all punctuation with spaces
				array_push($pattern, "/\W+/");
				//array_push($pattern, "/\d+/");
				
				//stripping out the noise words..
				foreach ( $noiseWords as $noiseWord ) {
					array_push($pattern, "/\b" . trim($noiseWord) . "\b/");
				}
				
				//now we reduce white space ..
				array_push($pattern,  "/\s+/");
				
				//run the preg's and replce with space :)
				$filtered = preg_replace($pattern," ", $filtered);
				
				//we then stuff it into an array 
				$documentWords = explode(" ", $filtered);
				$values = array();
				foreach ($documentWords as $documentWord) {
					if (strlen($documentWord) > 2){
						array_push($values, "('$documentCode', '" . trim( $documentWord  )."')");
					}
				}
				if (($count = count($values)) > 0) {
					$db->executeQuery("INSERT INTO searchkeyword (documentcode,word) VALUES " . implode(",",$values));
				}
				$message .= " $count keywords found\n";
			}
		}
		$message .= "</pre>";
		return $message;
	}
	
	function autoIndex($documentCode, $db) {
		// first find the document
		$tableWriter = new CmsTableWriter(CMS_FORMAT, $db);
		$message = "";
		if ( $tableWriter->resetCustomIterator(" WHERE documentcode='$documentCode'") && $tableWriter->iteratorItemCount > 0 ) {
			while (( $row = $tableWriter->nextItem() )) {
				$db->executeQuery("DELETE FROM searchkeyword WHERE documentcode='" .$row->documentcode . "'");
				if ($db->executeQuery("SELECT * FROM columnindex WHERE documenttypecode='$row->documenttypecode'") && $db->numRows() > 0 ) {
					
					$colsToIndex = array();
					while (($colRow = $db->nextObject())){
						
						array_push($colsToIndex, $colRow->columntoindex); 	
					}
					//we use a new table writer so we dont clobber the existing one ..
					$message .= indexDocument($documentCode, $colsToIndex, new CmsTableWriter(CMS_FORMAT, $db));
					unset($colsToIndex);
				}
			}
		}
		
		return $message;
	}

	// this may time out!!!!!
	function indexAllDocuments($db){
		$tableWriter = new CmsTableWriter(CMS_FORMAT,$db);
		$tableWriter->resetIterator();
		while (($row = $tableWriter->nextItem())){
			set_time_limit(30);
			print( autoIndex($row->documentcode, $db) );
		}
	}
	
	function keywordSearch($keyWords, $pageLimit, $pageNumber, $db){
		$keyWords = keyWordReduce($keyWords);
		
		// add a pageLimit check here:
		// we want to find the number of total result without pageLimit when we do the pagenations
		 
		//$db->executeQuery("SELECT count(word) as score, documentcode FROM searchkeyword WHERE  word IN ('" . implode("','", $keyWords) . "') GROUP BY documentcode ORDER BY score DESC LIMIT ". ($pageNumber * $pageLimit) ."," . ($pageLimit + 1));
		if($pageLimit)
			$db->executeQuery("SELECT count(word) as score, documentcode FROM searchkeyword WHERE  word IN ('" . implode("','", $keyWords) . "') GROUP BY documentcode ORDER BY score DESC LIMIT ". ($pageNumber * $pageLimit) ."," . ($pageLimit));
		else
			$db->executeQuery("SELECT count(word) as score, documentcode FROM searchkeyword WHERE  word IN ('" . implode("','", $keyWords) . "') GROUP BY documentcode ORDER BY score DESC ");		
		
		
		$results = array();
		while (($row = $db->nextObject())){
			array_push($results, $row->documentcode);
		}
		return $results;
	}

	
	//from cms gadgets.. stuff to do with indexing and searching
	
	function makeWordList($string){
		return explode( " ",strip_tags(reduceSpace($string)));
	}
	
	function makeKeyWordAbstract( $wordList, $keyWords) {
		if ( ($start = array_search($keyWords[0], $wordList)) === false ){
			$start = 0;
		} else {
			$start = $start - 5;
			if ($start < 0 ) { $start =0; }
		}
		return array_slice($wordList,$start,50);
	}

	function highlightKeyWords($string, $keyWordArray, $colourise = false){
		/*$colourIndex=0;
		$colourStyle="";
		$wordList = makeWordList($string, $keyWordArray);
		foreach ( $wordList as $index => $word ) {
			$cleanWord = wordClean($word);
			if (($keyIndex=array_search($cleanWord, $keyWordArray)) !== false) {
				if ($colourise){
					global $CMS_SEARCH_HIGHLIGHT_COLOURS;
					$colourStyle="class=\"highLight" . $keyIndex % count($CMS_SEARCH_HIGHLIGHT_COLOURS) . "\"";
				}
				$wordList[ $index ] = "<b " . $colourStyle . ">" .  $wordList[ $index ] . "</b>";
				$string = str_Ireplace("[\s]?" . preg_quote($word) . "[\s]", ("<b " . $colourStyle . "> " .  $word . " </b>"), $string);
			}
		}*/
		return $string;
	}


	
	//generates a pile of random coloured css directives
	function printHightlistStyleCSS(){
		global $CMS_SEARCH_HIGHLIGHT_COLOURS;
		global $CMS_SEARCH_TEXT_COLOURS;
		$seed = rand ( 0, count( $CMS_SEARCH_HIGHLIGHT_COLOURS ));
		for ( $i = (0 + $seed); $i < (count( $CMS_SEARCH_HIGHLIGHT_COLOURS ) + $seed) ; $i++) {
			echo ".highLight" . ($i - $seed) . " { background-color: " . $CMS_SEARCH_HIGHLIGHT_COLOURS[  ($i % count($CMS_SEARCH_HIGHLIGHT_COLOURS)) ] . "; color:  " . $CMS_SEARCH_TEXT_COLOURS[ ($i % count($CMS_SEARCH_HIGHLIGHT_COLOURS))  ] . " ; } \n";
		}
	}
	

	
	//prepares a list of keywords
	function keyWordReduce($string, $unique = true) {
		$string = wordClean( $string );
		$keyWords = explode(" ",$string);
		//get the noise words
		$noiseWords = file (CMS_SEARCH_NOISEWORDS);
		foreach ($noiseWords as $key => $value){
			$noiseWords[ $key ] = trim( $noiseWords[ $key ] );
		}
		//remove the noise words from the provided keywords
		foreach ($keyWords as $key => $value){
			if (in_array($value,$noiseWords) || strlen($value) <= 2 ){
				unset($keyWords[ $key ]);
			}
		}
		//strip duplicates here if we need to 
		if ($unique) {
			$keyWords = array_unique( $keyWords );
		}
		
		if (count($keyWords) > 10){
			for ($i= 10; $i < count($keyWords); $i++){
				unset($keyWords[ $i ]);
			}
		}
		
		return $keyWords;			
	}
	
	function wordClean( $string ) {
		$string = strtolower($string);
		$string = preg_replace("/[\W\s]+/"," ", $string);
		$string = reduceSpace($string);
		return trim($string);
	}
	
	//
?>